<script type="text/javascript">
  function sortClicked(){
      var selectBox = document.getElementById("sortBox");
      var selectedValue = selectBox.options[selectBox.selectedIndex].value;
      location.href = "lectures?sort="+selectedValue.toLowerCase();
  }
  function searchClicked(){
    var searchTerm = document.getElementById("searchBox");
    var searchedValue = searchTerm.value;
    location.href = "lectures?search="+searchedValue.toLowerCase();
  }
</script>

<div class="sectionTitle" xmlns="http://www.w3.org/1999/html">
  <h1>So many lectures!</h1>
</div>
<div class="edge">
  <img src="assets/edge.png"></img>
</div>

<form>
  <b>Sort by:</b>
  <select id="sortBox" name="sort">
    <option>Title</option>
    <option>Rating</option>
  </select>
  <button type="button" onclick="sortClicked();">Sort</button>
</form>

<form>
  <b>Search lecture: </b>
  <input type="text" name="search" id="searchBox">
  <button type="button" onclick="searchClicked();">Search</button>
</form>

<% @lectures.each do |lecture| %>
	<div class="lectureDetails">
		<p>
		    <span class="lectureTitle"><%= lecture.title.upcase %></span>

            <% avgRating = getAverage(lecture,"rating")%>

			<% (1..5).each do |i| %>
                <% if i <= avgRating then %>
 				    <%= image_tag("star.png", :alt => "star", :width => "30") %>
                <% else %>
                    <%= image_tag("starEmpty.png", :alt => "starEmpty", :width => "30") %>
                <% end %>
 			<% end %>

            <% lectureCount = lecture.lecture_ratings.count %>
            <% if lectureCount == 1 then %>
                (<%= lectureCount %> rating)
            <% else %>
                (<%= lectureCount %> ratings)
            <% end %>
		<p>

        <p>
          Difficulty:

          <% avgDiff = getAverage(lecture,"diff")%>

          <% (1..5).each do |i| %>
              <% if i <= avgDiff then %>
                  <%= image_tag("smart.png", :alt => "smart", :width => "15") %>
              <% else %>
                  <%= image_tag("smartNo.png", :alt => "smartNo", :width => "15") %>
              <% end %>
          <% end %>

          <br />

          Work load:

          <% avgWork = getAverage(lecture,"work")%>

          <% (1..5).each do |i| %>
              <% if i <= avgWork then %>
                  <%= image_tag("gearActive.png", :alt => "gearActive", :width => "15") %>
              <% else %>
                  <%= image_tag("gearInactive.png", :alt => "gearInactive", :width => "15") %>
              <% end %>
          <% end %>

        </p>

		<p>
			<%= lecture.location %> |
			ECTS: <%= lecture.ects %> |
		   	Track: <%= lecture.track %> |
		   	Code: <%= lecture.code %> |
		 	Sem: <%= lecture.semester %> |
		   	Language: <%= lecture.language %> <br />
			Lecture given by:
			<% lecture.professors.each_with_index do |prof, index| %>
                <% if index > 0 %>
                  <%= "and" %>
                <% end %>
				<b><%= prof.first_name %> <%= prof.last_name %></b>
		 	<% end %>
            <p>
		 	<% if current_user then %>
                <button type="button" class="shinyButton" onclick="location.href='/lecture_ratings/new/?lecture_id=<%= lecture.id %>'">Rate this lecture</button>
            <% end %>
            <button type="button" class="shinyButton" onclick="location.href='/comments/show/?lecture_id=<%= lecture.id %>'">Show comments</button>
            </p>
        <% if lecture.has_exercise == 1 then %>
            <span class="projectSection"> Project: </span>
            <div class="projectContainer">
                <div id="showProjectArrow">
                     <img src="assets/arrowDown.png"></img>
                </div>
                <div class="projectContent">
                  <% avgRating_project = getAverageProject(lecture,"rating")%>

                  <p>
                  <% (1..5).each do |i| %>
                      <% if i <= avgRating_project then %>
                          <%= image_tag("star.png", :alt => "star", :width => "30") %>
                      <% else %>
                          <%= image_tag("starEmpty.png", :alt => "starEmpty", :width => "30") %>
                      <% end %>
                  <% end %>

                  (<%= ExerciseRating.find_all_by_lecture_id(lecture.id).count%> ratings)

                  </p>

                  <p>Work load:
                  <% avgWork_project = getAverageProject(lecture,"work")%>

                  <% (1..5).each do |i| %>
                      <% if i <= avgWork_project then %>
                          <%= image_tag("gearActive.png", :alt => "gearActive", :width => "15") %>
                      <% else %>
                          <%= image_tag("gearInactive.png", :alt => "gearInactive", :width => "15") %>
                      <% end %>
                  <% end %>

                  Difficulty:

                  <% avgDiff_project = getAverageProject(lecture,"diff")%>

                  <% (1..5).each do |i| %>
                      <% if i <= avgDiff_project then %>
                          <%= image_tag("smart.png", :alt => "smart", :width => "15") %>
                      <% else %>
                          <%= image_tag("smartNo.png", :alt => "smartNo", :width => "15") %>
                      <% end %>
                  <% end %></p>
                  <% if current_user then %>
                  <button type="button" class="shinyButton" onclick="location.href='/exercise_ratings/new/?lecture_id=<%= lecture.id %>'">Rate this project</button>
				<% end %>	                
                </div>
             </div>
         <% end %>


        <% if current_user %>
            <p>
                <%= link_to 'Show', lecture %>
                <%= link_to 'Edit', edit_lecture_path(lecture) %>
                <%= link_to 'Destroy', lecture, confirm: 'Are you sure?', method: :delete %>
            </p>
        <% end %>

	</div><!-- lectureDetails -->
<% end %>

<script>
    var $jQuery = jQuery.noConflict();
    $jQuery("div#showProjectArrow").click(function() {
        $jQuery(this).next(".projectContent").toggle('fast');
    });
</script>

